package com.ruoyi.ai.service.impl;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.ai.domain.AiUserFollow;
import com.ruoyi.ai.domain.AiWork;
import com.ruoyi.ai.domain.AiWorkCollect;
import com.ruoyi.ai.mapper.AiUserFollowMapper;
import com.ruoyi.ai.service.IAiUserFollowService;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 关注管理Service业务层处理
 * 
 * @author tang
 * @date 2024-12-10
 */
@Service
public class AiUserFollowServiceImpl implements IAiUserFollowService
{
    @Autowired
    private AiUserFollowMapper aiUserFollowMapper;

    @Autowired
    private SysUserMapper sysUserMapper;


    @Override
    public List<SysUser> selectUserListByFollowerId(Long followerId) {
        QueryWrapper<AiUserFollow> wrapper=new QueryWrapper<>();
        List<SysUser> sysUsers=new ArrayList<>();
        wrapper.eq("follower_id", followerId);
        List<AiUserFollow> aiUserFollows = aiUserFollowMapper.selectList(wrapper);
        for (AiUserFollow aiUserFollow : aiUserFollows) {
            SysUser sysUser = sysUserMapper.selectUserById(aiUserFollow.getFolloweeId());
            if (sysUser!= null) {
                sysUsers.add(sysUser);
            }
        }
        return sysUsers;
    }

    @Override
    public List<SysUser> selectUserListByFolloweeId(Long followeeId) {
        QueryWrapper<AiUserFollow> wrapper=new QueryWrapper<>();
        List<SysUser> sysUsers=new ArrayList<>();
        wrapper.eq("followee_id", followeeId);
        List<AiUserFollow> aiUserFollows = aiUserFollowMapper.selectList(wrapper);
        for (AiUserFollow aiUserFollow : aiUserFollows) {
            SysUser sysUser = sysUserMapper.selectUserById(aiUserFollow.getFollowerId());
            if (sysUser!= null) {
                sysUsers.add(sysUser);
            }
        }
        return sysUsers;
    }

    @Override
    public int insertAiUserFollow(AiUserFollow aiUserFollow) {
        if (Objects.equals(aiUserFollow.getFolloweeId(), aiUserFollow.getFollowerId())){
            throw new ServiceException("你不能关注你自己");
        }
        QueryWrapper<AiUserFollow> wrapper=new QueryWrapper<>();
        wrapper.eq("follower_id", aiUserFollow.getFollowerId())
                .eq("followee_id", aiUserFollow.getFolloweeId());
        List<AiUserFollow> aiWorkCollects = aiUserFollowMapper.selectList(wrapper);
        if (aiWorkCollects.isEmpty()){
            return aiUserFollowMapper.insert(aiUserFollow);
        }
        return 0;

    }

    @Override
    public int deleteAiUserFollow(AiUserFollow aiUserFollow) {
        QueryWrapper<AiUserFollow> wrapper=new QueryWrapper<>();
        if (ObjectUtils.isNotEmpty(aiUserFollow.getFollowerId())||ObjectUtils.isNotEmpty(aiUserFollow.getFolloweeId())){
            wrapper.eq("follower_id", aiUserFollow.getFollowerId())
                    .eq("followee_id", aiUserFollow.getFolloweeId());
            return aiUserFollowMapper.delete(wrapper);
        }
        return 0;
    }
}
